package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.FP;
	
	public class Fracas extends Entity
	{
		[Embed(source = 'fracas.gif')] private const STANDING:Class;
		[Embed(source = 'images/fracas_fall.gif')] private const FALLING:Class;
		[Embed(source = 'images/fracas_flip.gif')] private const FLIPPING: Class;
		
		public var fracas_fall:Spritemap = new Spritemap(FALLING, 42, 40);
		public var fracas_flip:Spritemap = new Spritemap(FLIPPING, 42, 40);
		
		public var y_acceleration:int = new int;
		public var y_velocity:int = new int;
		public var x_acceleration:int = new int;
		public var x_velocity:int = new int;
		
		private var theIndex:int;
		public function Fracas() 
		{
			type = "fracas";
			y_acceleration = 0;
			x_acceleration = 0;
			
			layer = 0;
			y = -42;
			x = (Math.random() * 400) + 50;
			
			graphic = new Image(STANDING);
			setHitbox(42, 40);
			
			fracas_fall.add("fall", [0, 1], 9, true);
			fracas_flip.add("flip_right", [15, 14, 13, 12, 11, 10, 9, 8], 30, true);
			fracas_flip.add("flip_left", [0, 1, 2, 3, 4, 5, 6, 7], 30, true);
			graphic = fracas_fall;
			fracas_fall.play("fall");
		}
		
		override public function update():void {
			theIndex = fracasWorld.fraci.indexOf(this);
			//Fracas physics
			y_acceleration++;
			y = y + y_acceleration;
			x = x + x_acceleration;
			//wall bounce
			if (x < 0) {
				x = 0;
				x_acceleration = x_acceleration * -1;
				y_acceleration = y_acceleration - 5;
				fracasWorld.wallbounce[theIndex].theSize = 0;
				fracasWorld.wallbounce[theIndex].count = 30;
				fracasWorld.wallbounce[theIndex].x = x;
				if (y < 0) {
					fracasWorld.wallbounce[theIndex].y = 1;
				}else {
					fracasWorld.wallbounce[theIndex].y = y;
				}
				fracasWorld.wallbounce[theIndex].hits++;
				if (fracasWorld.wallbounce[theIndex].hits == 1) fracasWorld.wallbounce[theIndex].theText.text = "+200 \nbounce";
				if (fracasWorld.wallbounce[theIndex].hits == 2) fracasWorld.wallbounce[theIndex].theText.text = "+1500\ndouble";
				if (fracasWorld.wallbounce[theIndex].hits == 3) fracasWorld.wallbounce[theIndex].theText.text = "+5000\ntriple";
				if (fracasWorld.wallbounce[theIndex].hits  > 3) fracasWorld.wallbounce[theIndex].theText.text = "+10000\nM E G A";
			}
			if (x > FP.width - 42) {
				x = FP.width -42;
				x_acceleration = x_acceleration * -1;
				y_acceleration = y_acceleration - 5;
				fracasWorld.wallbounce[theIndex].theSize = 0;
				fracasWorld.wallbounce[theIndex].count = 30;
				fracasWorld.wallbounce[theIndex].x = x-40;
				if (y < 0) {
					fracasWorld.wallbounce[theIndex].y = 1;
				}else {
					fracasWorld.wallbounce[theIndex].y = y;
				}
				fracasWorld.wallbounce[theIndex].hits++;
				if (fracasWorld.wallbounce[theIndex].hits == 1) fracasWorld.wallbounce[theIndex].theText.text = "+200 \nbounce";
				if (fracasWorld.wallbounce[theIndex].hits == 2) fracasWorld.wallbounce[theIndex].theText.text = "+1500\ndouble";
				if (fracasWorld.wallbounce[theIndex].hits == 3) fracasWorld.wallbounce[theIndex].theText.text = "+5000\ntriple";
				if (fracasWorld.wallbounce[theIndex].hits  > 3) fracasWorld.wallbounce[theIndex].theText.text = "+10000\nM E G A";
			}
			//Girder collision
			if (collide("girder", x, y)) {
				var points:int = 0;
				points = (10 * (fracasWorld.streak+1));
				y_acceleration = -30;
				x_acceleration = x_acceleration - Math.floor(fracasWorld.girder.x_velocity/10);
				y = fracasWorld.girder.y - 42;
				//streak
				fracasWorld.streak++;
				fracasWorld.streaktext.count = 30;
				fracasWorld.streaktext.x = fracasWorld.girder.x + 10;
				fracasWorld.streaktext.y = fracasWorld.girder.y + 16;
				//points
				fracasWorld.girdertext.count = 20;
				fracasWorld.girdertext.x = x;
				fracasWorld.girdertext.y = y + 21;
				fracasWorld.girdertext.theText.text = "+"+points.toString();
				fracasWorld.girdertext.theText.size = 10 + Math.floor(points/10);
				fracasWorld.score = fracasWorld.score + points;
				//reset wallbounce
				fracasWorld.wallbounce[theIndex].hits = 0;
			}
			//Fracas spin
			if (x_acceleration > 2) {
				graphic = fracas_flip;
				fracas_flip.play("flip_right");
			}
			if (x_acceleration < -2) {
				graphic = fracas_flip;
				fracas_flip.play("flip_left");
			}
			if (x_acceleration >=-2 && x_acceleration <=2) {
				graphic = fracas_fall;
				fracas_fall.play("fall");
			}
			//Fracas death
			if (y > 600) {
				fracasWorld.streak = 0;
				fracasWorld.streaktext.count = 1;
				fracasWorld.fraci_in_play--;
				FP.world.remove(this);
			}
			
		}
	}

}